<?php
class Article extends MY_Controller {

       function __construct()
       {
            parent::__construct();
			$this->load->library('myfunction');   //载入自定义函数库
			@date_default_timezone_set('Etc/GMT+8');  //设置默认时区为GMT+8

       }
		
		//分类管理
		function manage_class() {
			$data['baseurl']=base_url();
			$this->load->model('article_model');
			$data['bigclass']= $this->article_model->get_bigclass_info();//读取大类别信息
			//print_r($data['bigclass']);
				foreach ($data['bigclass'] as $bigclass){
					$i=$bigclass['Id'];
					$data['smallclass'][$i]= $this->article_model->get_smallclass_info($i);
				}//多维数组操作，先读出大类ID，再查询对应二类，查询结果再写入数组供视图循环
			//print_r($data['smallclass']);
			$this->load->view('manage/manage_class_view',$data);
		}

		//添加大类
		function add_bigclass() {
			$data['baseurl']=base_url();
			$data['title']='增加大类';
			$data['values']='';
			$data['action']=base_url().'manage/article/do_add_bigclass';
			$this->load->view('manage/bigclass_info_view',$data);
		}
		
		//添加大类do
		function do_add_bigclass() {
			$bc_name=$this->myfunction->mf_trim($this->input->post('bigclass'));
			if(strlen($bc_name)!=0) {
				$this->load->model('article_model');
				if($this->article_model->add_bigclass_info($bc_name)!="") {
					$data['msg']='新增大类别成功！';
					$data['rurl']=base_url().'manage/article/manage_class';
					$this->load->view('manage/jsmsgbox_view',$data);
				}else{
					$data['msg']='新增大类别失败，请与管理员联系！';
					$data['rurl']=base_url().'manage/article/manage_class';
					$this->load->view('manage/jsmsgbox_view',$data);
				}
			}else{
				$data['msg']='类别名称不能为空！';
				$data['rurl']=base_url().'manage/article/add_bigclass';
				$this->load->view('manage/jsmsgbox_view',$data);
			}
		}
		
		//删除大类do
		function do_del_bigclass() {
			$id=$this->uri->segment(4);
			$this->load->model('article_model');
			$this->article_model->del_bigclass_info($id);
			$data['msg']='大类别删除成功！';
			$data['rurl']=base_url().'manage/article/manage_class';
			$this->load->view('manage/jsmsgbox_view',$data);
		}
		
		//删除二级分类do
		function do_del_smallclass() {
			$sc_id=$this->uri->segment(5);
			$this->load->model('article_model');
			$this->article_model->sc_del_smallclass($sc_id);
			$data['msg']='二级类别删除成功！';
			$data['rurl']=base_url().'manage/article/manage_class';
			$this->load->view('manage/jsmsgbox_view',$data);
		}


		//编辑大类
		function edit_bigclass() {
			$id=$this->uri->segment(4);
			$data['baseurl']=base_url();
			$data['title']='编辑大类';
			$this->load->model('article_model');
			$data['values']=$this->article_model->get_onebigclass_info($id);
			$data['action']=base_url().'manage/article/do_edit_bigclass/'.$id;
			$this->load->view('manage/bigclass_info_view',$data);
		}

		//编辑大类do
		function do_edit_bigclass() {
			$bc_id=$this->uri->segment(4);
			$bc_name=$this->myfunction->mf_trim($this->input->post('bigclass'));
			if(strlen($bc_name)!=0) {
				$this->load->model('article_model');
				if($this->article_model->edit_bigclass_info($bc_id,$bc_name)==0){
					$data['msg']='类别修改成功！';
					$data['rurl']=base_url().'manage/article/manage_class';
					$this->load->view('manage/jsmsgbox_view',$data);
				}else{
					$data['msg']='类别修改失败，请与技术支持联系！';
					$data['rurl']=base_url().'manage/article/manage_class';
					$this->load->view('manage/jsmsgbox_view',$data);
				}
			}else{
				$data['msg']='类别名称不能为空！';
				$data['rurl']=base_url().'manage/article/edit_bigclass/'.$bc_id;
				$this->load->view('manage/jsmsgbox_view',$data);
			}
		}

		//添加二级分类
		function add_smallclass() {
			$data['baseurl']=base_url();
			$data['title']='增加二级类别';
			$this->load->model('article_model');
			$data['list']=$this->article_model->get_bigclass_info();//获取大类列表
			$data['b_values']=$this->uri->segment(4);
			$data['s_values']='';
			$data['action']=base_url().'manage/article/do_add_smallclass';
			$this->load->view('manage/smallclass_info_view',$data);
		}

		//添加二级分类do
		function do_add_smallclass() {
			$bc_id=$this->input->post('bigclass');
			$sc_name=$this->myfunction->mf_trim($this->input->post('smallclass'));
			if(strlen($sc_name)!=0) {
				$this->load->model('article_model');
				if($this->article_model->add_smallclass_info($bc_id,$sc_name)!="") {
					$data['msg']='新增二级类别成功！';
					$data['rurl']=base_url().'manage/article/manage_class';
					$this->load->view('manage/jsmsgbox_view',$data);
				}else{
					$data['msg']='新增二级类别失败，请与管理员联系！';
					$data['rurl']=base_url().'manage/article/manage_class';
					$this->load->view('manage/jsmsgbox_view',$data);
				}
			}else{
				$data['msg']='类别名称不能为空！';
				$data['rurl']=base_url().'manage/article/add_smallclass';
				$this->load->view('manage/jsmsgbox_view',$data);
			}
		}

		//编辑二级分类
		function edit_smallclass() {
			$data['baseurl']=base_url();
			$data['title']='编辑二级类别';
			$this->load->model('article_model');
			$data['list']=$this->article_model->get_bigclass_info();//获取大类列表
			$data['s_values']=$this->article_model->get_onesmallclass_info($this->uri->segment(5));//获取小类名称
			$data['b_values']=$this->uri->segment(4);
			$data['action']=base_url().'manage/article/do_edit_smallclass/'.$this->uri->segment(4).'/'.$this->uri->segment(5);
			$this->load->view('manage/smallclass_info_view',$data);
		}

		//编辑二级分类do
		function do_edit_smallclass() {
			$bc_id=$this->input->post('bigclass');
			$bc_id_old=$this->uri->segment(4);
			$sc_id=$this->uri->segment(5);
			$sc_name=$this->myfunction->mf_trim($this->input->post('smallclass'));
			if(strlen($sc_name)!=0) {
				$this->load->model('article_model');
				if($bc_id_old!=$bc_id){
					$this->article_model->edit_smallclass_info($sc_id,$sc_name);//修改二级类别
					$this->article_model->change_bigclass($sc_id,$bc_id);//修改二级类别所属大类
					$this->article_model->change_bigclass_article($bc_id,$sc_id);//修改所属文章大类别
				}else{
					$this->article_model->edit_smallclass_info($sc_id,$sc_name);//修改二级类别
				}
				$data['msg']='类别修改成功！';
				$data['rurl']=base_url().'manage/article/manage_class';
				$this->load->view('manage/jsmsgbox_view',$data);
			}else{
				$data['msg']='类别名称不能为空！';
				$data['rurl']=base_url().'manage/article/edit_smallclass/'.$bc_id_old.'/'.$sc_id;
				$this->load->view('manage/jsmsgbox_view',$data);
			}

		}


		//添加文章
	    function add_article(){
			$data['baseurl']=base_url();
			$data['title']='添加文章';
			$data['count']=0;
			$this->load->model('article_model');
			$data['bigclass']= $this->article_model->get_bigclass_info();//读取所有大类别信息
			$data['smallclass']= $this->article_model->get_allsmallclass_info();//读取所有二级类别信息
			$this->load->model('hospital_model');//载入医院模型
			$data['hospital']= $this->hospital_model->get_hospital_list($_SESSION['admin_info']['user_areacode']);//读取医院列表信息
			$data['action']=base_url().'manage/article/do_add_article';
			$params = array('name' => 'editor');
			$this->load->library('SinaEditor', $params);
			$this->sinaeditor->value = '';
			$info = $this->sinaeditor->create();
			$data['editor'] = $info;
			$this->load->view('manage/add_article_view.php',$data);
		}


		//添加文章do
	    function do_add_article(){
			$data['baseurl']=base_url();
			if($this->input->post('fileisok')!='') {
				$config['upload_path'] = './uploads/indeximg';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['max_size'] = '500';
				$config['max_width']  = '1024';
				$config['max_height']  = '768';
				$config['encrypt_name']='TRUE';
				$this->load->library('upload', $config);
				$this->upload->do_upload();
				$data = array('upload_data' => $this->upload->data());  //上传文件
				$imgname=$data['upload_data']['file_name']; 
				$this->do_image($imgname);//调用生成缩略图
			}else{
				$imgname='';
			}
			$article['areacode']=$_SESSION['admin_info']['user_areacode'];//发布当前文章的地区代码
			$article['bigclass']=$this->input->post('bigclass');
			$article['smallclass']=$this->input->post('smallclass');
			$article['title']=$this->input->post('title');
			$article['image']= $imgname;
			$article['author']=$this->input->post('author');
			$article['summary']=$this->myfunction->safe_convert($this->input->post('summary'));
			$article['content']=$this->input->post('editor');
			$article['tags']=$this->input->post('tags');
			$article['hospital']=$this->input->post('hospital');
			$article['puttime']=now();//当前时间（东8区）
			$this->load->model('article_model');
			if(($this->article_model->add_article($article))==0){
				$data['msg']='文章添加成功！';
				$data['rurl']=base_url().'manage/article/list_article';
				$this->load->view('manage/jsmsgbox_view',$data);
			}
		}

		//生成缩略图
		function do_image($imgname){
			$this->load->library('image_lib');
			$config['image_library'] = 'gd2';
			$config['source_image'] = "./uploads/indeximg/".$imgname;
			$config['new_image'] = "./uploads/indeximg/thumb/".$imgname;
			$config['width'] = 48;
			$config['height'] = 48;
			$this->image_lib->initialize($config);
			$this->image_lib->resize();
		}


		//文章列表
		function list_article() {
			$data['baseurl']=base_url();
			$page_num = '10';//每页的数据
			$this->load->model('article_model');
			$allarticle= $this->article_model->get_article_list($_SESSION['admin_info']['user_areacode'],$page_num,$this->uri->segment(4));//读取该分站下的所有文章(分页输出)
			//print_r($data['article']);
			$total_nums=$this->article_model->all_article_num($_SESSION['admin_info']['user_areacode']);//读取文章数
			$data['article']=$allarticle;
			$data['num']=$total_nums;
			$this->load->library('pagination');
			$config['base_url'] = base_url().'manage/article/list_article/';
			$config['uri_segment'] = 4;
			$config['total_rows'] = $total_nums;//总共多少条数据
			$config['per_page'] = $page_num;//每页显示几条数据
			$config['first_link'] = '首页';
			$config['first_tag_open'] = '<li>';//“第一页”链接的打开标签。
			$config['first_tag_close'] = '</li>';//“第一页”链接的关闭标签。
			$config['last_link'] = '尾页';//你希望在分页的右边显示“最后一页”链接的名字。
			$config['last_tag_open'] = '<li>';//“最后一页”链接的打开标签。
			$config['last_tag_close'] = '</li>';//“最后一页”链接的关闭标签。
			$config['next_link'] = '下一页';//你希望在分页中显示“下一页”链接的名字。
			$config['next_tag_open'] = '<li>';//“下一页”链接的打开标签。
			$config['next_tag_close'] = '</li>';//“下一页”链接的关闭标签。
			$config['prev_link'] = '上一页';//你希望在分页中显示“上一页”链接的名字。
			$config['prev_tag_open'] = '<li>';//“上一页”链接的打开标签。
			$config['prev_tag_close'] = '</li>';//“上一页”链接的关闭标签。
			$config['cur_tag_open'] = '<li class="current">';//“当前页”链接的打开标签。
			$config['cur_tag_close'] = '</li>';//“当前页”链接的关闭标签。
			$config['num_tag_open'] = '<li>';//“数字”链接的打开标签。
			$config['num_tag_close'] = '</li>';
			$this->pagination->initialize($config);
			$this->load->view('manage/list_article_view',$data);
		}

		//编辑文章
		function edit_article() {
			$data['baseurl']=base_url();
			$id=$this->uri->segment(4);
			if($this->ck_right('ac',$id)==0) {//验证权限是否为高级管理员或本地区分站管理
				$this->load->model('article_model');
				$article=$this->article_model->get_one_article($id);
				if($article['sc_id']!=0) {
					$data['sc_from_bc']=$this->article_model->get_smallclass_info($article['bc_id']);//如果有二级类别的返回大类别下所有二级类别
				}
				
				$data['title']='编辑文章';
				$data['count']=0;
				$this->load->model('article_model');
				$data['bigclass']= $this->article_model->get_bigclass_info();//读取所有大类别信息
				$data['smallclass']= $this->article_model->get_allsmallclass_info();//读取所有二级类别信息
				$this->load->model('hospital_model');//载入医院模型
				$data['hospital']= $this->hospital_model->get_hospital_list($_SESSION['admin_info']['user_areacode']);//读取医院列表信息
				$data['action']=base_url().'manage/article/do_edit_article/'.$id;
				$params = array('name' => 'editor');
				$this->load->library('SinaEditor', $params);
				$this->sinaeditor->value = $article['content'];
				$info = $this->sinaeditor->create();
				$data['editor'] = $info;
				$data['article']=$article;
				$this->load->view('manage/edit_article_view',$data);
			}else{
				$data['msg']='无权操作，不能编辑此文章！';
				$data['rurl']=base_url().'manage/article/list_article';
				$this->load->view('manage/jsmsgbox_view',$data);
			}
		}

		//编辑文章do
	    function do_edit_article(){
			$id=$this->uri->segment(4);
			$data['baseurl']=base_url();
			if($this->input->post('fileisok')!='') {
				$config['upload_path'] = './uploads/indeximg';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['max_size'] = '500';
				$config['max_width']  = '1024';
				$config['max_height']  = '768';
				$config['encrypt_name']='TRUE';
				$this->load->library('upload', $config);
				$this->upload->do_upload();
				$data = array('upload_data' => $this->upload->data());  //上传文件
				$imgname=$data['upload_data']['file_name']; 
				$this->do_image($imgname);//调用生成缩略图
			}else{
				$imgname='';
			}
			$article['bigclass']=$this->input->post('bigclass');
			$article['smallclass']=$this->input->post('smallclass');
			$article['title']=$this->input->post('title');
			$article['image']= $imgname;
			$article['author']=$this->input->post('author');
			$article['summary']=$this->myfunction->safe_convert($this->input->post('summary'));
			$article['content']=$this->input->post('editor');
			$article['tags']=$this->input->post('tags');
			$article['hospital']=$this->input->post('hospital');
			$article['puttime']=now();//当前时间（东8区）
			$this->load->model('article_model');
			if(($this->article_model->edit_article($id,$article))==0){
				$data['msg']='文章更新成功！';
				$data['rurl']=base_url().'manage/article/list_article';
				$this->load->view('manage/jsmsgbox_view',$data);
			}
		}

		//删除文章
		function do_del_article() {
			$id=$this->uri->segment(4);
			if($this->ck_right('ac',$id)==0) {//验证权限是否为高级管理员或本地区分站管理
				$this->load->model('article_model');
				$this->article_model->del_article($id);
				$data['msg']='文章删除成功！';
				$data['rurl']=base_url().'manage/article/list_article';
				$this->load->view('manage/jsmsgbox_view',$data);
			}else{
				$data['msg']='无权操作，文章删除失败！';
				$data['rurl']=base_url().'manage/article/list_article';
				$this->load->view('manage/jsmsgbox_view',$data);
			}
		}

		//添加讲座
		function add_lecture() {
			$data['baseurl']=base_url();
			$data['title']='添加讲座信息';
			$data['action']=base_url().'manage/article/do_add_lecture';
			$this->load->view('manage/add_lecture_view',$data);
		}

		//添加讲座do
	    function do_add_lecture(){
			$data['baseurl']=base_url();
			if($this->input->post('fileisok')!='') {
				$config['upload_path'] = './uploads/lecimg';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['max_size'] = '500';
				$config['max_width']  = '1024';
				$config['max_height']  = '768';
				$config['encrypt_name']='TRUE';
				$this->load->library('upload', $config);
				$this->upload->do_upload();
				$data = array('upload_data' => $this->upload->data());  //上传文件
				$imgname=$data['upload_data']['file_name']; 
			}else{
				$imgname='';
			}
			$lec['areacode']=$_SESSION['admin_info']['user_areacode'];//发布当前讲座的地区代码
			$lec['lec_title']=$this->input->post('lec_title');
			$lec['lec_time']=$this->input->post('lec_time');
			$lec['lec_address']=$this->input->post('lec_address');
			$lec['lec_image']= $imgname;
			$lec['lec_intro']=$this->myfunction->safe_convert($this->input->post('lec_intro'));
			$lec['lec_tel']=$this->input->post('lec_tel');
			$lec['puttime']=now();//当前时间（东8区）
			$this->load->model('article_model');
			if(($this->article_model->add_lecture($lec))==0){
				$data['msg']='讲座添加成功！';
				$data['rurl']=base_url().'manage/article/list_lecture';
				$this->load->view('manage/jsmsgbox_view',$data);
			}
		}

		//讲座列表
		function list_lecture() {
			$data['baseurl']=base_url();
			$page_num = '10';//每页的数据
			$this->load->model('article_model');
			$alllecture= $this->article_model->get_all_lecture($_SESSION['admin_info']['user_areacode'],$page_num,$this->uri->segment(4));//读取该分站下的所有讲座信息(分页输出)
			$total_nums=$this->article_model->all_lecture_num($_SESSION['admin_info']['user_areacode']);//读取文章数
			$data['lecture']=$alllecture;
			$data['num']=$total_nums;
			$this->load->library('pagination');
			$config['base_url'] = base_url().'manage/article/list_lecture/';
			$config['uri_segment'] = 4;
			$config['total_rows'] = $total_nums;//总共多少条数据
			$config['per_page'] = $page_num;//每页显示几条数据
			$config['first_link'] = '首页';
			$config['first_tag_open'] = '<li>';//“第一页”链接的打开标签。
			$config['first_tag_close'] = '</li>';//“第一页”链接的关闭标签。
			$config['last_link'] = '尾页';//你希望在分页的右边显示“最后一页”链接的名字。
			$config['last_tag_open'] = '<li>';//“最后一页”链接的打开标签。
			$config['last_tag_close'] = '</li>';//“最后一页”链接的关闭标签。
			$config['next_link'] = '下一页';//你希望在分页中显示“下一页”链接的名字。
			$config['next_tag_open'] = '<li>';//“下一页”链接的打开标签。
			$config['next_tag_close'] = '</li>';//“下一页”链接的关闭标签。
			$config['prev_link'] = '上一页';//你希望在分页中显示“上一页”链接的名字。
			$config['prev_tag_open'] = '<li>';//“上一页”链接的打开标签。
			$config['prev_tag_close'] = '</li>';//“上一页”链接的关闭标签。
			$config['cur_tag_open'] = '<li class="current">';//“当前页”链接的打开标签。
			$config['cur_tag_close'] = '</li>';//“当前页”链接的关闭标签。
			$config['num_tag_open'] = '<li>';//“数字”链接的打开标签。
			$config['num_tag_close'] = '</li>';
			$this->pagination->initialize($config);
			$this->load->view('manage/list_lecture_view',$data);
		}

		//编辑讲座信息
		function edit_lecture() {
			$data['baseurl']=base_url();
			$id=$this->uri->segment(4);
			if($this->ck_right('le',$id)==0) {//验证权限是否为高级管理员或本地区分站管理
				$this->load->model('article_model');
				$lec = $this->article_model->get_one_lecture($id);
				$data['lec']=$lec[0];
				$data['title']='编辑讲座信息';
				$data['action']=base_url().'manage/article/do_edit_lecture/'.$id;
				$this->load->view('manage/edit_lecture_view',$data);
			}else{
				$data['msg']='无权操作，不能编辑此讲座信息！';
				$data['rurl']=base_url().'manage/article/list_lecture';
				$this->load->view('manage/jsmsgbox_view',$data);
			}
		}

		//编辑讲座do
	    function do_edit_lecture(){
			$data['baseurl']=base_url();
			$id=$this->uri->segment(4);
			if($this->input->post('fileisok')!='') {
				$config['upload_path'] = './uploads/lecimg';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['max_size'] = '500';
				$config['max_width']  = '1024';
				$config['max_height']  = '768';
				$config['encrypt_name']='TRUE';
				$this->load->library('upload', $config);
				$this->upload->do_upload();
				$data = array('upload_data' => $this->upload->data());  //上传文件
				$imgname=$data['upload_data']['file_name']; 
			}else{
				$imgname='';
			}
			$lec['lec_title']=$this->input->post('lec_title');
			$lec['lec_time']=$this->input->post('lec_time');
			$lec['lec_address']=$this->input->post('lec_address');
			$lec['lec_tel']=$this->input->post('lec_tel');
			$lec['lec_image']= $imgname;
			$lec['lec_intro']=$this->myfunction->safe_convert($this->input->post('lec_intro'));
			$lec['puttime']=now();//当前时间（东8区）
			$this->load->model('article_model');
			if(($this->article_model->edit_lecture($id,$lec))==0){
				$data['msg']='讲座修改成功！';
				$data['rurl']=base_url().'manage/article/list_lecture';
				$this->load->view('manage/jsmsgbox_view',$data);
			}
		}

		//删除讲座
		function do_del_lecture() {
			$id=$this->uri->segment(4);
			if($this->ck_right('le',$id)==0) {//验证权限是否为高级管理员或本地区分站管理
				$this->load->model('article_model');
				$this->article_model->del_lecture($id);
				$data['msg']='讲座信息删除成功！';
				$data['rurl']=base_url().'manage/article/list_lecture';
				$this->load->view('manage/jsmsgbox_view',$data);
			}else{
				$data['msg']='无权操作，讲座信息删除失败！';
				$data['rurl']=base_url().'manage/article/list_lecture';
				$this->load->view('manage/jsmsgbox_view',$data);
			}
		}

}
?>